<!--  -->
<template>
  <div class="">
    <el-button type="danger" @click="batchRemove">批量删除</el-button>
    <el-tree
      :data="data"
      :props="defaultProps"
      @node-click="handleNodeClick"
      :expand-on-click-node="false"
      show-checkbox
      node-key="id"
      ref="tree"
      :default-expanded-keys="checkData"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level < 3"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            添加
          </el-button>
          <el-button type="text" size="mini" @click="() => update(data)">
            修改
          </el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="category" ref="category">
        <el-form-item label="名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="Icon">
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位">
          <el-input
            v-model="category.productUnit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add"> 确 定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "",
      dialogType: "",
      dialogUrl: "",
      dialogFormVisible: false,
      category: {
        catId: null,
        name: "",
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        icon: "",
        productUnit: "",
      },
      reset: {
        catId: null,
        name: "",
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        icon: "",
        productUnit: "",
      },
      data: [],
      checkData: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    append(data) {
      this.category = Object.assign({}, this.reset);
      console.log(data);
      this.dialogFormVisible = true;
      this.category.parentCid = data.id;
      this.category.catLevel = data.catLevel + 1;
      this.dialogType = "add";
      this.dialogUrl = "/product/category/save";
      this.title = "新增";
    },
    add() {
      console.log(this.category);
      this.$http({
        url: this.$http.adornUrl(this.dialogUrl),
        method: "post",
        data: this.$http.adornData(this.category, false),
      })
        .then(({ data }) => {
          this.$message({
            type: "success",
            message: this.title + "成功!",
          });
          this.getDataList();
          this.checkData = [this.category.parentCid];
          this.dialogFormVisible = false;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消" + this.title,
          });
        });
    },
    update(data) {
      this.category = Object.assign({}, this.reset);
      this.$http({
        url: this.$http.adornUrl("/product/category/info/" + data.id),
        method: "post",
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        this.category.name = data.category.name;
        this.category.catId = data.category.catId;
        this.category.parentCid = data.category.parentCid;
        this.category.catLevel = data.category.catLevel;
        this.category.icon = data.category.icon;
        this.category.productUnit = data.category.productUnit;
      });
      this.dialogFormVisible = true;
      this.dialogType = "update";
      this.dialogUrl = "/product/category/update";
      this.title = "修改";
    },
    remove(node, data) {
      let ids = [data.id];
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getDataList();
            this.checkData = [node.parent.data.id];
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    batchRemove() {
      let checkNodes = this.$refs.tree.getCheckedNodes()
      let ids = [];
      for(let key in checkNodes){
        ids.push(checkNodes[key].id)
      }
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getDataList();
            this.checkData = [node.parent.data.id];
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        this.data = data.data;
      });
    },
    handleNodeClick(data) {
      console.log(data);
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
  created() {
    this.getDataList();
  },
};
</script>
<style scoped>
/* @import url(); 引入公共css类 */
</style>
